(function (w) {
    if ('addEventListener' in document) {
        document.addEventListener('DOMContentLoaded', function() {
            FastClick.attach(document.body);
        }, false);
    }

    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    });

    var url = "http://api.lcb520.cn:8000/api/GlottoGame/GetRecodeList?page=1&rows=50";
    var global = {};
    global.btnable = true;
    global.sortType = "order"; // order || desc
    global.chartData = [];
    global.red_space = [];
    global.blue_space = [];

    global.scrollParam = {
        scrollX: true,
        probeType: 3,
        momentum: false,
        bounce: false,
        freeScroll: true,
        scrollbars: true,
        fadeScrollbars: true
    };

    global.getChartData = function (callback) {
        var xhr = new XMLHttpRequest();

        xhr.open("get", url);

        //var formData = new FormData();
        // formData.append('lotno', lotno);
        // formData.append('pageNum', 1);
        // formData.append('pageSize', 50);
        xhr.send();

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var response = JSON.parse(xhr.responseText.replace(/\\/g, ''));
                if (response.StatusCode == 1) {
                    //global.chartData = response.twininfoMissingsList.reverse();
                    global.chartData = response.Data.Data.reverse();
                    global.red_space = response.Data.Space.split('|')[0].split(',');
                    global.blue_space = response.Data.Space.split('|')[1].split(',');
                    callback && callback(global.chartData,response.Data.Number);
                }
                
            }
        };
    };

    global.scrollHandler = function (boxClass) {
        var scrollBox = new IScroll(boxClass + " .wrapper", global.scrollParam);

        var elements = global.getScrollElements(boxClass);

        if (global.sortType === "order") {
            global.scrollToEnd(scrollBox, elements);
        } else {
            global.scrollToTop(scrollBox, elements);
        }

        scrollBox.on('scroll', global.addScroll.bind(scrollBox, elements));
    };

    global.scrollToEnd = function (scroll, elements) {
        var positionY = scroll.wrapperHeight - scroll.scrollerHeight;
        scroll.scrollTo(0, positionY);
        elements.issueList.style.webkitTransform = "translate(0," + positionY + "px) translateZ(0px)";
        elements.issueList.style.mozTransform = "translate(0," + positionY + "px) translateZ(0px)";
        elements.issueList.style.msTransform = "translate(0," + positionY + "px) translateZ(0px)";
        elements.issueList.style.oTransform = "translate(0," + positionY + "px) translateZ(0px)";
        elements.issueList.style.transform = "translate(0," + positionY + "px) translateZ(0px)";
    };

    global.scrollToTop = function (scroll, elements) {
        scroll.scrollTo(0, 0);
        elements.issueList.style.webkitTransform = "translate(0,0) translateZ(0px)";
        elements.issueList.style.mozTransform = "translate(0,0) translateZ(0px)";
        elements.issueList.style.msTransform = "translate(0,0) translateZ(0px)";
        elements.issueList.style.oTransform = "translate(0,0) translateZ(0px)";
        elements.issueList.style.transform = "translate(0,0) translateZ(0px)";
    };


    global.addScroll = function (elements) {
        if (elements.titleBox) {
            elements.titleBox.style.webkitTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.titleBox.style.mozTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.titleBox.style.msTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.titleBox.style.oTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.titleBox.style.transform = "translate(" + this.x + "px, 0) translateZ(0px)";
        }

        if (elements.chooseBox) {
            elements.chooseBox.style.webkitTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.chooseBox.style.mozTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.chooseBox.style.msTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.chooseBox.style.oTransform = "translate(" + this.x + "px, 0) translateZ(0px)";
            elements.chooseBox.style.transform = "translate(" + this.x + "px, 0) translateZ(0px)";
        }

        if (elements.issueList) {
            elements.issueList.style.webkitTransform = "translate(0," + this.y + "px) translateZ(0px)";
            elements.issueList.style.mozTransform = "translate(0," + this.y + "px) translateZ(0px)";
            elements.issueList.style.msTransform = "translate(0," + this.y + "px) translateZ(0px)";
            elements.issueList.style.oTransform = "translate(0," + this.y + "px) translateZ(0px)";
            elements.issueList.style.transform = "translate(0," + this.y + "px) translateZ(0px)";
        }
    };


    global.getScrollElements = function (boxClass) {
        return {
            titleBox: document.querySelector(boxClass + " .title-content"),
            chooseBox: document.querySelector(boxClass + " .choose-row"),
            issueList: document.querySelector(boxClass + " .issue-list")
        };
    };

    global.bet = function (obj) {
        var params = [];
        for (var key in obj) {
            params.push(key + "=" + obj[key]);
        }
        console.log(params.join("&"));

        window.postMessage(params.join("&"));
    };

    function setBetBtnEnable(btnable) {
        global.btnable = btnable;
        var betBtn = document.querySelector(".bet-btn");
        if (!global.btnable) {
            betBtn.classList.add("bet-btn-disable");
        }
    }

    document.addEventListener("message", function (msg) {
        if (msg.data.indexOf("setBenEnable") > -1) {
            setBetBtnEnable(msg.data.indexOf("true") > -1);
        } else if (msg.data.indexOf("sortType") > -1) {
            if (msg.data.indexOf("desc") > -1) {
                global.sortType = "desc";
            } else {
                global.sortType = "order";
            }
        } else {
            location.reload();
        }
    });

    w.global = global;
})(window);


